package UserInterface;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import GameEngine.Game;
import Main.Main;

@SuppressWarnings("serial")
/**
 * Screen that is displayed during the game play
 */
public class GameScreen extends JPanel implements MouseListener{
	private ImageIcon background;
	private JTextField highScoreInput;
	private JButton highScoreButton;
	

	/**
	 * Initializes the game screen with the given background image.
	 */
	public GameScreen(ImageIcon img){
		background = img;
		this.addMouseListener(this);
		this.setLayout(null);
		highScoreInput = new JTextField();
		highScoreInput.setVisible(false);
		highScoreInput.setSize(80, 25);
		highScoreButton = new JButton();
		highScoreButton.setVisible(false);
		highScoreButton.setSize(120, 25);
		highScoreButton.setLabel("Submit");
		this.add(highScoreInput);
		this.add(highScoreButton);
		highScoreInput.setLocation(290, 230);
		highScoreButton.setLocation(240, 290);
		highScoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                String userName = Main.GUI.getGameScreen().getHighScoreInput().getText();
                int currentScore = Main.mygame.getBoat().getScore();
                String location = Main.GUI.getScoreScreen().sendRequest("http://j.maxmind.com/app/country.js", "");
                location = location.split("'")[1];
                String URL = "http://www.bciproject.com/cs319/highScore2.asp?submit=1&name="+userName+"&location="+location+"&score="+currentScore;
                Main.GUI.getScoreScreen().sendRequest(URL, "");
                Main.mygame = new Game();
                Main.GUI.setActiveScreen("scoreScreen");
                Main.GUI.setGameScreen(new GameScreen(Main.GUI.getImages().getRiverBackgroundImage()));
                Main.GUI.getScoreScreen().showHighScore();
            }
        });
		
	}
	

	/**
	 * Draws the each scene of the game
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Image image = background.getImage();
		g.drawImage(image, 0, 0, null);
		
		if (Main.mygame.isStarted())
		{
			double overflow = Main.mygame.getOverflow()*2.2;
			if (overflow==0)
			{
				overflow = 44;
			}
			String[] mapRows = Main.mygame.getRiverController().getMap().split(",");
			for(int i = 0; i < 12; i++){
				String currentRow = mapRows[i];
				for( int k = 0; k < 10 ; k++){
					if (currentRow.substring(k,k+1).equals("#")){
						g.drawImage(Main.mygame.getRiverController().getRiverObjects().get(1).getIco().getImage(),k*50+100,(int) (i*44+overflow),50,44, null);
					}
					else if(currentRow.substring(k,k+1).equals("+")){
						g.drawImage(Main.mygame.getRiverController().getRiverObjects().get(3).getIco().getImage(),k*50+100,(int) (i*44+overflow),50,44, null);
					}
					else if(currentRow.substring(k,k+1).equals("_")){
						g.drawImage(Main.mygame.getRiverController().getRiverObjects().get(2).getIco().getImage(),k*50+100,(int) (i*44+overflow),50,44, null);
					}
					else if(!currentRow.substring(k,k+1).equals(".")){
						g.drawImage(Main.mygame.getRiverController().getRiverObjects().get(0).getIco().getImage(),k*50+100,(int) (i*44+overflow),50,44, null);
						g.setFont(new Font("Arial", Font.BOLD, 13));
						g.drawString(Integer.parseInt(currentRow.substring(k,k+1))*100+"", k*50+113, (int) (i*44+overflow+28));
					}
				}
			}
		}

		Main.mygame.getBoat().renderBoat(g);
		Main.mygame.renderGamePanel(g);
		if(Main.mygame.isSpeedUp()){
			g.setColor(Color.ORANGE);
			g.setFont(new Font("Helvetica", Font.BOLD, 35));
			g.drawString("SPEED UP", 300, 80);
		}

		
		if (Main.mygame.isGameOver())
		{
			g.setColor(new Color(Color.BLACK.getRed(), Color.BLACK.getGreen(), Color.BLACK.getBlue(), 120));
			g.fillRect(0, 0, 700, 519);
			g.setColor(Color.RED);
			g.setFont(new Font("Helvetica", Font.BOLD, 30));
			g.drawString("GAME OVER", 240, 130);
			
			g.setColor(new Color(Color.BLACK.getRed(), Color.BLACK.getGreen(), Color.BLACK.getBlue(), 120));
			g.fillRect(200, 190, 250, 130);
			g.setColor(Color.WHITE);
			g.setFont(new Font("Helvetica", Font.BOLD, 22));
			g.drawString("Submit Score", 240, 210);
			g.setFont(new Font("Helvetica", Font.PLAIN, 20));
			g.drawString("Name:", 220, 250);
			g.drawString("Score: "+Main.mygame.getBoat().getScore(), 220, 277);
			highScoreInput.setVisible(true);
			highScoreButton.setVisible(true);
		}
		this.repaint();
	}

	

	public void setBackground(ImageIcon background) {
		this.background = background;
	}

	public JTextField getHighScoreInput() {
		return highScoreInput;
	}

	public void setHighScoreInput(JTextField highScoreInput) {
		this.highScoreInput = highScoreInput;
	}

	public JButton getHighScoreButton() {
		return highScoreButton;
	}

	public void setHighScoreButton(JButton highScoreButton) {
		this.highScoreButton = highScoreButton;
	}

	@Override
	public void mouseClicked(MouseEvent e) {		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {		
	}

	@Override
	public void mousePressed(MouseEvent e) {		
	}

	@Override
	public void mouseReleased(MouseEvent e) {		
	}

}
